<?php if (!defined("BASEPATH")) exit("No direct script access allowed"); ?>
<?
$sub = (($this->uri->segment(4) == "COU")?("COU"):(($this->uri->segment(4) == "CITY")?("CITY"):("INS")));
?>
<script type="text/javascript">
	Ext.onReady(function() {

		Ext.namespace('basicDataSponsor');
		basicDataSponsor.createSlider = function(id, name, value) {
			new Ext.form.SliderField({
				width: 100,
				renderTo: id,
				value: value,
				name: name,
				id: name,
				tipText: function(thumb){
					return String(thumb.value) + '%';
				},
				listeners: {
					changecomplete: function(slider, value, thumb) {

						Ext.Ajax.request({url: '<?=site_url()?>admin/<?=(($sub == "COU")?("country"):(($sub == "CITY")?("city"):("institute")))?>_insert_update', params: 'slider=1&<?=strtolower($sub)?>Id=' + slider.getId().replace('slider_', '').replace('-slider', '') + '&val=' + value, success: function() {
							
							}
						});

					}
				}
			})
		}

		var store = new Ext.data.JsonStore({
			totalProperty: 'total', 
			root: 'results', 
			sortInfo: {field: '<?=strtolower($sub)?>Name', direction: 'ASC'},
			remoteSort: true,
			proxy : new Ext.data.HttpProxy({
				url: '<?=site_url()?>admin/get_<?=(($sub == "COU")?("country"):(($sub == "CITY")?("city"):("institute")))?>',
				method: 'GET'
			}),
			<?
			switch($sub) {
				case "COU": { echo "baseParams: {fields: '[\"couPresenter\"]', query: 'I'},"; } break;
				case "CITY": { echo "baseParams: {fields: '[\"cityPresenter\"]', query: 'I'},"; } break;
				case "INS": { echo "baseParams: {fields: '[\"insPresenter\"]', query: 'I'},"; } break;
			}
			?>
			fields: [
				<? if($sub == "COU") { ?>
				{name: 'couId'},
				{name: 'couName'},
				{name: 'couUrlName'},
				{name: 'couConId'},
				{name: 'conName'},
				{name: 'couPriority'}
				<? } elseif($sub == "CITY") { ?>
				{name: 'cityId'},
				{name: 'cityName'},
				{name: 'cityUrlName'},
				{name: 'cityCouId'},
				{name: 'couName'},
				{name: 'cityPriority'}
				<? } else { ?>
				{name: 'insId'},
				{name: 'insName'},
				{name: 'insUrlName'},
				{name: 'insCityId'},
				{name: 'insPriority'},
				{name: 'cityName'},
				{name: 'couName'}
				<? }  ?>
			]
		});

		var grid = new Ext.grid.GridPanel({
			store: store,
			clicksToEdit: 1,
			viewConfig:{
				emptyText: '<?=$this->db->escape_str($this->lang->line("THERE_IS_CURRENTLY_NO_DATA_TO_DISPLAY"))?>'
			},
			columns: [
				<? if($sub == "COU") { ?>
				{id: '<?=strtolower($sub)?>Name', header: '<?=$this->db->escape_str($this->lang->line("COUNTRY"))?>', width: 200, sortable: true, dataIndex: '<?=strtolower($sub)?>Name'},
				<? } elseif($sub == "CITY") { ?>
				{id: '<?=strtolower($sub)?>Name', header: '<?=$this->db->escape_str($this->lang->line("CITY"))?>', width: 200, sortable: true, dataIndex: '<?=strtolower($sub)?>Name', renderer: function(v, cell, record) { return record.data.cityName + ' (' + record.data.couName + ')' }},
				<? } else { ?>
				{id: '<?=strtolower($sub)?>Name', header: '<?=$this->db->escape_str($this->lang->line("INSTITUTE"))?>', width: 200, sortable: true, dataIndex: '<?=strtolower($sub)?>Name', renderer: function(v, cell, record) { return record.data.insName + ' (' + record.data.cityName + ', ' + record.data.couName + ')' }},
				<? }  ?>

				{header: '<?=$this->db->escape_str($this->lang->line("PRIORITIES"))?>', width: 150, sortable: false, dataIndex: '<?=strtolower($sub)?>Priority', renderer: function(v, cell, record) {

					var contentId = Ext.id();
					basicDataSponsor.createSlider.defer( 1, this, [contentId, 'slider_' + record.data.<?=strtolower($sub)?>Id, v]);
					return '<div id="' + contentId + '"></div>'; 

					}
				}
			],
/*
			listeners: {
				afteredit: function(e) {
					console.info(e);
					//commitChanges()
				}
			},
*/
			autoExpandColumn: '<?=strtolower($sub)?>Name',
			stripeRows: true,
			autoHeight: true,
			autoWidth: true,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),

			tbar: {
				items: [
					
					]
			},

			border: false,

			frame:false,

			plugins:[
				new Ext.ux.grid.Search({
					minChars:2,
					iconCls: 'icon-zoom',
					searchText:'<?=$this->db->escape_str($this->lang->line("SEARCH"))?>',
					position:'top',
					minCharsTipText:'<?=$this->db->escape_str($this->lang->line("MINIMUM_TWO_CHARACTERS_IS_REQUIRED"))?>',
					selectAllText: '<?=$this->db->escape_str($this->lang->line("SELECT_ALL"))?>',
					width:200,
					disableIndexes:[],
					autoFocus:true
				})]

		});

		grid.render('grid-table-sponsor-<?=$sub?>');

		grid.getSelectionModel().on('selectionchange', function(sm){
			
		});

		store.load();

	})
</script>

<div id="grid-table-sponsor-<?=$sub?>"></div>